Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 19/23 Β· 86.1 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
}
// Using a lambda to call the method
runCalculation((number, otherNumber) -> number + otherNumber);
// Equivalent code which uses an anonymous class instead
runCalculation(new Calculation() {
@Override
public int calculate(int someNumber, int someOtherNumber) {
return someNumber + someOtherNumber;
}
})

Lambda's parameters types do not have to be fully specified and can be inferred from the interface it implements. Lambda's body can be written without a body block and a return statement if it is only an expression. Also, for those interfaces which only have a single parameter in the method, round brackets can be omitted.cite-ref-8[8]

// Same call as above, but with fully specified types and a body block
runCalculation((int number, int otherNumber) -> {
return number + otherNumber;
});
// A functional interface with a method which has only a single parameter
interface StringExtender {
String extendString(String input);
}
// Initializing a variable of this type by using a lambda
StringExtender extender = input -> input + " Extended";

Method references

It is not necessary to use lambdas when there already is a named method compatible with the interface. This method can be passed instead of a lambda using a method reference. There are several types of method references:

| Reference type | Example | Equivalent lambda |
|---|---|---|
| Static | Integer::sum | (number, otherNumber) -> number + other… |
| Bound | "LongString"::substring | index -> "LongString".substring(index) |
| Unbound | String::isEmpty | string -> string.isEmpty() |
| Class constructor | ArrayList<String>::new | capacity -> new ArrayList<String>(capac… |
| Array constructor | String[]::new | size -> new String[size] |

The code above which calls runCalculation could be replaced with the following using the method references:

runCalculation(Integer::sum);

Inheritance

Interfaces can inherit from other interfaces just like classes. Unlike classes it is allowed to inherit from multiple interfaces. However, it is possible that several interfaces have a field with the same name, in which case it becomes a single ambiguous member, which cannot be accessed.

/* Class implementing this interface must implement methods of both
ActionListener and RequestListener */
interface EventListener extends ActionListener, RequestListener {
}

Default methods

Java SE 8 introduced default methods to interfaces which allows developers to add new methods to existing interfaces without breaking compatibility with the classes already implementing the interface. Unlike regular interface methods, default methods have a body which will get called in the case if the implementing class does not override it.

interface StringManipulator {
String extendString(String input);
// A method which is optional to implement
default String shortenString(String input) {
return input.substring(1);
}
}
// This is a valid class despite not implementing all the methods
class PartialStringManipulator implements StringManipulator {
@Override
public String extendString(String input) {
return input + " Extended";
}
}

Static methods

Static methods is another language feature introduced in Java SE 8. They behave in exactly the same way as in the classes.

interface StringUtils {

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────